package com.bytz.modules.cms.domain.impl.order;

import cn.hutool.core.util.BooleanUtil;
import com.bytz.modules.cms.common.model.CmsConfig;
import com.bytz.modules.cms.common.service.CmsConfigService;
import com.bytz.modules.cms.domain.MallOrderProcessor;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.order.constant.OrderConstants;
import com.bytz.modules.cms.order.model.OrderItemModel;
import com.bytz.modules.cms.order.model.OrderModel;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 订单默认值处理器 3
 * @author shanxuezhong
 */
public class MallOrderDefaultValueProcessor extends MallOrderAbstractProcessor implements MallOrderProcessor {


    private CmsConfigService configService;
    @Autowired
    public void setConfigService(CmsConfigService configService) {
        this.configService = configService;
    }

    public MallOrderDefaultValueProcessor(int sequence) {
        super(sequence);
    }

    @Override
    public void doProcess(MallOrderProcessContext context) {
        OrderModel order = context.getOrder();
        CmsConfig globalConfig = configService.getGlobal();

        order.setSupplierName(globalConfig.getSupplierUnitName());
        order.setSupplierAddress(globalConfig.getSupplierUnitAddress());
        order.setSupplierPostcode(globalConfig.getSupplierPostcode());
        order.setSignedPlace(globalConfig.getSignedPlace());
        order.setSupplierPhone(globalConfig.getSupplierPhone());
        order.setSupplierFax(globalConfig.getSupplierFax());
        order.setTaxRate(globalConfig.getTaxRate());
        order.setCurrency(globalConfig.getCurrency());
        order.setSupplierSignedDate(null);
        order.setPaidPrice(BigDecimal.ZERO);
        order.setDeductionPrice(null);
        order.setResellerId(context.getResellerId());

        if (BooleanUtil.isTrue(context.getSubmit())) {
            order.setStatus(OrderConstants.OrderStatus.YI_TI_JIAO);
            order.setDemanderSignedDate(LocalDateTime.now());
            order.setPayType(globalConfig.getDefaultPayType());
            order.setWarrantyPeriod(globalConfig.getDefaultWarrantyPeriod());
        }

        if (BooleanUtil.isTrue(context.getStandardOrder())) {
            order.setProjectName(null);
        }

        for (OrderItemModel item : order.getOrderItemList()) {
            item.setOrderId(null);
            item.setShippedNum(0);
            item.setPaidPrice(BigDecimal.ZERO);

            if (BooleanUtil.isTrue(context.getStandardOrder())) {
                item.setNegotiatePrice(null);
            }
        }
    }
}